<template>
  <div class="panel-top">
    <div class="item-title clearfix">
      <div class="name fl">{{$t('header.nav.statistics')}}</div>
    </div>
    <div class="item-content">
      <div class="item">
        <div class="item-top">
          <spinner v-if="initLoading"></spinner>
          <template v-else>
            <cu :val="shares | num" :decimals="2" />
            <span class="unit">{{shares_unit}}{{coinHashUnit}}/s</span>
          </template>
        </div>
        <div class="item-bottom">
          <span class="name">{{$t('statistics.rate')}}</span>
        </div>
      </div>
      <div class="item">
        <div class="item-top">
          <spinner v-if="initLoading"></spinner>
          <cu v-else :val="workers_active | num" :decimals="0" />
        </div>
        <div class="item-bottom">
          <span class="name">{{$t('statistics.worker_online')}}</span>
        </div>
      </div>
      <div class="item">
        <div class="item-top">
          <spinner v-if="initLoading"></spinner>
          <cu v-else :val="height | num" :decimals="0" />
        </div>
        <div class="item-bottom">
          <span class="name">{{$t('statistics.block_in')}}</span>
        </div>
      </div>
      <div class="item">
        <div class="item-top">
          <spinner v-if="initLoading"></spinner>
          <template v-else>
            <cu :val="qu_time | num" :decimals="0" />
            <span class="unit">{{qu_time_unit}}</span>
          </template>
        </div>
        <div class="item-bottom">
          <span class="name">{{$t('statistics.block_time')}}</span>
        </div>
      </div>
      <div class="item">
        <div class="item-top">
          <spinner v-if="initLoading"></spinner>
          <template v-else>
            <cu :val="difficulty | num" :decimals="3" />
            <span class="unit">{{difficulty_unit}}</span>
          </template>
        </div>
        <div class="item-bottom">
          <span class="name">{{$t('statistics.difficulty')}}</span>
        </div>
      </div>
      <div class="item" v-if="coin_type!=='dcr' && coin_type!=='bch' && coin_type!=='bsv' && coin_type!=='zec'">
        <div class="item-top">
          <spinner v-if="initLoading"></spinner>
          <template v-else>
            <cu :val="workload | num" :decimals="2" />
            <span class="unit">%</span>
          </template>
        </div>
        <div class="item-bottom">
          <span class="name">{{$t('statistics.current_work')}}</span>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import Spinner from '@/components/Spinner';
import Cu from '@/components/CountUp';
import { InPoolStatus } from '@/api';

export default {
  name: 'panelTop',
  props: {
    coin_type: {
      type: String,
      default: '',
    },
  },
  components: {
    Spinner,
    Cu,
  },
  data() {
    return {
      initLoading: true,
      loading: false,
      shares: 0,
      shares_unit: '',
      workers_active: 0,
      height: '',
      qu_time: 0,
      qu_time_unit: 's',
      difficulty: '0',
      difficulty_unit: 'P',
      workload: 0,
      timer: undefined,
    };
  },
  computed: {
    coinHashUnit() {
      return this.coin_type === 'zec' ? 'Sol' : 'H';
    },
  },
  created() {
    this.getInPoolStatus();
    this.timer = window.setInterval(() => {
      this.getInPoolStatus();
    }, 15000);
  },
  beforeDestroy() {
    window.clearInterval(this.timer);
  },
  methods: {
    getInPoolStatus() {
      if (this.loading || !this.coin_type) {
        return;
      }
      this.loading = true;
      InPoolStatus({ coin_type: this.coin_type }).then((res) => {
        if (res.data.status === 1) {
          const data = res.data.data;
          this.shares = data.shares;
          this.shares_unit = data.shares_unit;
          this.workers_active = data.workers_active;
          this.height = data.height;
          this.qu_time = data.qu_time;
          this.qu_time_unit = data.qu_time_unit;
          this.difficulty = data.difficulty;
          this.difficulty_unit = data.difficulty_unit;
          this.workload = data.workload.slice(0, data.workload.length - 1);
        }
        this.loading = false;
        this.initLoading = false;
      }).catch(() => {
        this.loading = false;
        this.initLoading = false;
      });
    },
  },
};
</script>

